package com.masterclient.app.businessmodule.vipmodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.utils.TextUtils;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.utils.GlideUtil;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.vipmodule.bean.ServiceOrderDetail;
import com.masterclient.app.businessmodule.vipmodule.persenter.SellerRefundContract;
import com.masterclient.app.businessmodule.vipmodule.persenter.SellerRefundPresenter;
import com.masterclient.app.utils.CommonUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 售后订单-订单详细
 */
public class ServiceOrderDetailActivity extends FCBusinessActivity<SellerRefundPresenter> implements SellerRefundContract.View {

    @BindView(R.id.container_product)
    LinearLayout containerProduct;
    @BindView(R.id.step_iv)
    ImageView stepIv;
    @BindView(R.id.negotiation_tv)
    TextView negotiationTv;
    @BindView(R.id.supplier_tv)
    TextView supplierTv;
    @BindView(R.id.total_tv)
    TextView totalTv;
    @BindView(R.id.jinka_tv)
    TextView jinkaTv;
    @BindView(R.id.youhui_tv)
    TextView youhuiTv;
    @BindView(R.id.coupon_tv)
    TextView couponTv;
    @BindView(R.id.num_tv)
    TextView numTv;
    @BindView(R.id.amount_tv)
    TextView amountTv;
    @BindView(R.id.btn1_tv)
    TextView btn1Tv;
    @BindView(R.id.btn2_tv)
    TextView btn2Tv;
    @BindView(R.id.container_content)
    LinearLayout containerContent;
    @BindView(R.id.btn_view)
    LinearLayout btnView;
    @BindView(R.id.root_view)
    NestedScrollView rootView;
    @BindView(R.id.error_iv)
    ImageView errorIv;
    @BindView(R.id.error_tv)
    TextView errorTv;
    @BindView(R.id.empty_view)
    LinearLayout emptyView;
    private String refund_no;
    private String id;// 订单ID
    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_service_detail;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        refund_no = getIntent().getStringExtra("refund_no");
        String title = getIntent().getStringExtra("title");
        navigationBar.setTitle(title);
        mPresenter.sellerRefundDetail(refund_no);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        View view1 = LayoutInflater.from(this).inflate(R.layout.detail_product_item, null);
        view1.findViewById(R.id.tk_tv).setVisibility(View.GONE);
        containerProduct.addView(view1);
        View view2 = LayoutInflater.from(this).inflate(R.layout.detail_product_item, null);
        view2.findViewById(R.id.tk_tv).setVisibility(View.GONE);
        containerProduct.addView(view2);
        setOrderStatus();
    }

    /**
     * 设置订单状态
     */
    private void setOrderStatus() {
//        int status = order.getOrder().getStatus();
//        if(0 == status){// 待审核
//            stepIv.setImageResource(R.drawable.icon_service_step1);
//            setBtn("同意申请","拒绝申请");
//        }else if(1 == status){// 待买家退货
//            stepIv.setImageResource(R.drawable.icon_service_step4);
//            btnView.setVisibility(View.GONE);
//        }else if(2 == status){// 待卖家收货
//            stepIv.setImageResource(R.drawable.icon_service_step5);
//            setBtn("","确认收货");
//        }else if(3 == status){// 待退款
//            stepIv.setImageResource(R.drawable.icon_service_step1);
//            setBtn("同意退款","拒绝退款");
//        }else if(4 == status){// 退款成功
//            stepIv.setImageResource(R.drawable.icon_service_step2);
//            setBtn("","删除订单");
//        }else if(5 == status){// 已关闭
//            int closeReason = order.getCloseReason();
//            if(1 ==closeReason)
//                spztView.setVisibility(View.GONE);
//            stepIv.setImageResource(R.drawable.icon_service_step3);
//            setBtn("","删除订单");
//        }
    }

    /**
     * 设置按钮文字
     * @param text1
     * @param text2
     */
    private void setBtn(String text1,String text2){
        btnView.setVisibility(View.VISIBLE);
        if(TextUtils.isEmpty(text1))
            btn1Tv.setVisibility(View.GONE);
        else btn1Tv.setVisibility(View.VISIBLE);
        btn1Tv.setText(text1);
        btn2Tv.setText(text2);
    }

    @OnClick({R.id.kefu_view, R.id.tel_view, R.id.btn1_tv, R.id.btn2_tv,R.id.negotiation_tv
    ,R.id.empty_view})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.kefu_view:
                break;
            case R.id.tel_view:
                break;
            case R.id.btn1_tv:
                break;
            case R.id.btn2_tv:
                break;
            case R.id.negotiation_tv:
                startActivity(new Intent(this,NegotiationActivity.class));
                break;
            case R.id.empty_view:
                rootView.setVisibility(View.GONE);
                emptyView.setVisibility(View.GONE);
                mPresenter.sellerRefundDetail(refund_no);
                break;
        }
    }

    @Override
    public void succ(ServiceOrderDetail bean) {
        if (null != bean)
            setOrder(bean);
    }

    /**
     * 设置订单详情信息
     *
     * @param sellerOrderDetail
     */
    private void setOrder(ServiceOrderDetail sellerOrderDetail) {
        emptyView.setVisibility(View.GONE);
        rootView.setVisibility(View.VISIBLE);
        id = sellerOrderDetail.getId();
//        // 按钮信息
//        SellerOrderDetail.ButtonBean buttonBean = sellerOrderDetail.getButton();
//        setBtn(buttonBean);
        // 用户信息
//        SellerOrderDetail.BuyerInfoBean buyerInfoBean = sellerOrderDetail.getBuyer_info();
//        String headImg = buyerInfoBean.getShow_headimgurl();
//        if (null != headImg)
//            GlideUtil.displayImage(this, headImg, headIv, R.mipmap.user_head_default_icon);
//        nameTv.setText(buyerInfoBean.getShow_name());
//        int level = buyerInfoBean.getLevel();
//        mobile = buyerInfoBean.getMobile();
//        String text = "<font color=\"#333333\">" + CommonUtil.getLevetText(level) + "</font>　" + mobile;
//        agentTv.setText(Html.fromHtml(text));
        // 商品信息
        List<ServiceOrderDetail.ProductsBean> productsList = sellerOrderDetail.getProducts();
        containerProduct.removeAllViews();
        for (ServiceOrderDetail.ProductsBean products : productsList) {
            View view = addProductItem(products);
            containerProduct.addView(view);
        }
//        String remark = sellerOrderDetail.getOrder_remark();
//        if (!android.text.TextUtils.isEmpty(remark))
//            beizhuTv.setText(remark);
//        // 优惠信息
//        List<SellerOrderDetail.SummaryInfoBean> summaryInfoBeans = sellerOrderDetail.getSummary_info();
//        containerSummary.removeAllViews();
//        if (null != sellerOrderDetail) {
//            for (SellerOrderDetail.SummaryInfoBean summaryInfoBean : summaryInfoBeans) {
//                View view = addSummaryItem(summaryInfoBean);
//                if (null != view)
//                    containerSummary.addView(view);
//            }
//        }
//        numTv.setText("共 " + sellerOrderDetail.getProduct_num() + " 件产品");
//        amountTv.setText("￥" + sellerOrderDetail.getSubTotal());
//        sfkTv.setText("￥" + sellerOrderDetail.getSubTotal());
        // 添加订单编号信息
        List<ServiceOrderDetail.ContentArrBean> contentArrBeanS = sellerOrderDetail.getContent_arr();
        containerContent.removeAllViews();
        if (null != contentArrBeanS) {
            for (ServiceOrderDetail.ContentArrBean contentArrBean : contentArrBeanS) {
                View view = addContentItem(contentArrBean);
                containerContent.addView(view);
            }
        }
//        // 退款
//        String refund_amount = sellerOrderDetail.getRefund_amount();
//        if (!"0".equals(refund_amount)) {
//            tkjeView.setVisibility(View.VISIBLE);
//            tuikTv.setText("￥" + refund_amount);
//        }
//        // 收入到账
//        String income = sellerOrderDetail.getIncome();
//        if (!"0".equals(income)) {
//            srdzView.setVisibility(View.VISIBLE);
//            daoztv.setText("￥" + income);
//        }
//        // 下级佣金
//        String commission = sellerOrderDetail.getCommission();
//        if (!"0".equals(commission)) {
//            xjyjView.setVisibility(View.VISIBLE);
//            yongjinTv.setText("￥" + commission);
//        }
//        // 状态信息
//        statusIv.setImageResource(R.drawable.icon_order_dfh);
//        SellerOrderDetail.TipBean tipBean = sellerOrderDetail.getTip();
//        statusTv.setText(tipBean.getLabel());
//        explainTv.setText(tipBean.getText());
//        shifu.setText("实付:");
//        if (status == 0) {// 待付款
//            shifu.setText("金额:");
//            statusIv.setImageResource(R.drawable.icon_order_unpay);
//        } else if (status == 3) {// 已关闭
//            statusTv.setTextColor(getResources().getColor(R.color.color_F1444D));
//            statusIv.setImageResource(R.drawable.icon_order_close);
//        } else if (status == 8) {// 已完成订单
//            statusIv.setImageResource(R.drawable.icon_complete);
//        } else if (status == 9) {// 已移交订单
//            statusIv.setImageResource(R.drawable.icon_complete);
//        }
    }

    @Override
    public void error(String error) {
        emptyView.setVisibility(View.VISIBLE);
        rootView.setVisibility(View.GONE);
        errorTv.setText(error);
    }

    @Override
    public void orderPushsucc(String msg) {

    }

    /**
     * @return 动态添加产品信息
     */
    private View addProductItem(ServiceOrderDetail.ProductsBean products) {
        View view = LayoutInflater.from(this).inflate(R.layout.detail_product_item, null);
        ImageView imageView = view.findViewById(R.id.product_iv);
        String img = products.getImage();
        if (null != img)
            GlideUtil.displayImage(this, img, imageView, R.drawable.product_default);
        TextView nameTv = view.findViewById(R.id.title_tv);
        nameTv.setText(products.getName());
        TextView priceTv = view.findViewById(R.id.price_tv);
        priceTv.setText("￥" + products.getAmount());
        TextView numTv = view.findViewById(R.id.num_tv);
        numTv.setText("x" + products.getQty_ordered());
        TextView specificationsTv = view.findViewById(R.id.specifications_tv);
        specificationsTv.setText(products.getAttribute_desc());
        return view;
    }


    /**
     * 添加订单编号信息
     *
     * @param contentArrBean
     * @return
     */
    private View addContentItem(ServiceOrderDetail.ContentArrBean contentArrBean) {
        View view = LayoutInflater.from(this).inflate(R.layout.detail_content_item, null);
        TextView labelTv = view.findViewById(R.id.label_tv);
        TextView textTv = view.findViewById(R.id.text_tv);

        String label = contentArrBean.getLabel();
        String text = contentArrBean.getText();
        boolean copyable = contentArrBean.isCopyable();
        labelTv.setText(label + ":");
        textTv.setText(text);
        if (copyable) {
            textTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_copy, 0);
            view.setOnClickListener(v -> CommonUtil.copy(this, text));
        }
        return view;
    }

}
